package com.study.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.study.services.WeixiuService;
import com.study.services.WeixiuTableService;

@Controller
/**
 * 维修总控台
 * 
 * @author Administrator
 *
 */
public class WeixiuTableController {
	@Autowired
	WeixiuTableService service;
	@Autowired
	WeixiuService weixiuservice;

	/**
	 * 查看当前的维修订单
	 */
	@PostMapping("/afterSale/weixiuTable")
	@ResponseBody
	public List<JSONObject> weixiuByDay(Date time) {
		System.out.println("我进来了。" + time.toLocaleString());
		List<JSONObject> object = service.queryByDay(time.toLocaleString());
		System.out.println("ss" + object);
		return object;
	}

	/**
	 * 查询当天需要维修工单(第一次进入维修工单)
	 */
	@PostMapping("/afterSale/weixiuTablety")
	@ResponseBody
	public String queryAll(Integer type, @RequestParam(required = false, defaultValue = "") Date date,
			Integer currentPage, HttpSession session) {
		Integer userid = (Integer) session.getAttribute("useid");
		if (userid == null) {
			return null;
		} else {
			List<JSONObject> object = service.queryAll(date, type, currentPage, 8,userid+"");
			System.out.println("条件：" + type + "时间：" + date + "当前页：" + currentPage);
			PageInfo<JSONObject> pager = new PageInfo<JSONObject>(object);
			String json = JSONObject.toJSONString(pager);
			return json;
		}
	}

	/**
	 * 查看维修详情+维修配件
	 */
	@GetMapping("/weixiuTabledetail")
	public ModelAndView queryById(HttpServletRequest request, HttpSession session) {
		String id = request.getParameter("id");

		JSONObject object = service.queryById(id);
		ModelAndView mv = new ModelAndView("/afterSale/weixiuTableDetail");
		mv.addObject("weixiuTabledetail", object);

		JSONArray attribute = (JSONArray) session.getAttribute("PeiJianCart");

		if (attribute == null || attribute.isEmpty()) {
			JSONArray array = service.getPeiJianCart(id);
			System.out.println(id + "我是从数据库出来的" + array);
			session.setAttribute("PeiJianCart", array);
		}
		System.out.println(attribute);
		return mv;
	}

	/**
	 * 修改维修状态为 执行中
	 */
	@PostMapping("/afterSale/changeState")
	@ResponseBody
	public boolean changeState(String id,Integer cusid ,HttpSession session) {
		Integer useid = (Integer) session.getAttribute("useid");
		boolean result = service.changeState(id, 461, null, null,useid,cusid);
		return result;
	}

	/**
	 * 选择商品配件
	 */
	@GetMapping("/selectGood")
	public ModelAndView selectGood(HttpServletRequest request, HttpSession session) {
		// String repId = request.getParameter("id");
		// session.setAttribute("repId", repId);
		ModelAndView mv = new ModelAndView("/afterSale/addPeiJian");
		mv.addObject("weixiugood", weixiuservice.findGoods());// 查询所有配件
		return mv;
	}

	/**
	 * 保存维修配件
	 */
	@PostMapping("/saveRepairgoods")
	@ResponseBody
	public String saveRepairgoods(String xx, String sum, String number, HttpSession session) {
		JSONArray goods = (JSONArray) JSONArray.parse(xx);

		JSONObject object = new JSONObject();
		object.put("sum", sum);
		object.put("number", number);

		goods.add(0, object);
		// String repId = (String)session.getAttribute("repId");
		// boolean result = service.addPeiJian(repId,xx);
		// System.out.println(repId+xx);
		if (!"".equals(xx)) {
			session.setAttribute("PeiJianCart", goods);
			System.out.println("我进来啦");
		}
		return "ok";
	}

	/**
	 * 修改维修状态为待客户确认+保存维修配件单
	 */
	@PostMapping("/changeStateByFinish")
	@ResponseBody
	public boolean changeStateByFinish(String id, Date date, String sum,Integer cusid, HttpSession session) {
		JSONArray attribute = (JSONArray) session.getAttribute("PeiJianCart");
		Integer useid = (Integer) session.getAttribute("useid");
		// 修改执行状态
		boolean result1 = service.changeState(id, 465, date, sum,useid,cusid);
		System.out.println(date + sum + "我是完成时间+金额" + id);
		// 修改完成时间
		// boolean result3 = service.changeFinishTime(id,date);
		// 修改总金额

		// 添加维修配件单
		boolean result2 = true;
		if (attribute.size() > 0) {
			result2 = service.addPeiJian(id, attribute);
		}

		// 删除回话
		session.removeAttribute("PeiJianCart");
		if (result1 && result2) {
			System.out.println("修改成功");
			return true;
		} else {
			System.out.println("修改失败");
			return false;
		}
	}

}
